﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.TextFormatting;
using ZuesControls.Commons;
using ZuesControls.Helpers;
using ZuesControls.Interfaces;
using ZuesControls.Mvvm;

namespace ZuesControls.Controls
{
    public class ZSProgressBar : ProgressBar
    {
        public ICommand CancelClickCommand { get; }
        public Button PART_CloseButton { get; set; }
        public ZSProgressBar()
        {

            CancelClickCommand = new RelayCommand<Tuple<object, MouseButtonEventArgs>>(ClickEvent);
        }
        // 注册路由事件
        public static readonly RoutedEvent ProgressBarEventEvent = EventManager.RegisterRoutedEvent(
            "ProgressBarEvent",
            RoutingStrategy.Bubble, // 或者Direct、Tunnel
            typeof(EventHandler<ProgressBarEventArgs>),
            typeof(ZSProgressBar));

        // CLR 包装器
        public event EventHandler<ProgressBarEventArgs> ProgressBarEvent
        {
            add { AddHandler(ProgressBarEventEvent, value); }
            remove { RemoveHandler(ProgressBarEventEvent, value); }
        }



        public string ExplainTextProperty
        {
            get { return (string)GetValue(ExplainTextPropertyProperty); }
            set { SetValue(ExplainTextPropertyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ExplainTextProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ExplainTextPropertyProperty =
            DependencyProperty.Register("ExplainTextProperty", typeof(string), typeof(ZSProgressBar), new PropertyMetadata("这是说明"
                ));



        private void ClickEvent(Tuple<object, MouseButtonEventArgs>? tuple)
        {
            DependencyObject sender = tuple.Item2.OriginalSource as DependencyObject;
            Button button = VisualHelper.TryFindParent<Button>(sender);
            ProgressBarEventArgs args = new ProgressBarEventArgs(ProgressBarEventEvent, this, button, tuple.Item2);
            RaiseEvent(args);
        }

        static ZSProgressBar()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ZSProgressBar), new FrameworkPropertyMetadata(typeof(ZSProgressBar)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            PART_CloseButton = this.GetTemplateChild(nameof(PART_CloseButton)) as Button;
        }

      
    }

    public class ProgressBarEventArgs : RoutedEventArgs
    {
        public Button Btn { get; }
        public MouseButtonEventArgs mouseButtonEventArgs { get; }

        public ProgressBarEventArgs(RoutedEvent routedEvent, object source, Button btn, MouseButtonEventArgs e)
            : base(routedEvent, source)
        {
            mouseButtonEventArgs = e;
            Btn = btn;
        }
    }
}
